package org.abc.fund.controller.dataSource;

import lombok.RequiredArgsConstructor;
import org.abc.fund.entity.funds.FundBasicInfo;
import org.abc.fund.service.dataSourceCollection.FundUniverseService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/api/v1/funds")
@RequiredArgsConstructor
public class FundUniverseController {
    private final FundUniverseService fundUniverseService;
    /**
     * 初始化基金池，从数据源获取全市场基金列表并保存指定数量到数据库
     * @param request 请求体，包含聚宽用户名、密码和要初始化的基金数量
     * @return 保存成功的基金列表
     */
    @PostMapping("/initialize-universe")
    public ResponseEntity<?> initializeFundUniverse(@RequestBody Map<String, Object> request) {

        String username = (String) request.get("username");
        String password = (String) request.get("password");
        Integer limit = (Integer) request.getOrDefault("limit", 150);
        @SuppressWarnings("unchecked")
        List<String> fundTypes = (List<String>) request.get("fund_types");

        if (username == null || password == null) {
            return ResponseEntity.badRequest().body(Map.of(
                    "status", "error",
                    "message", "Username and password are required."
            ));
        }

        try {
            Map<String, Object> serviceResult = fundUniverseService.initializeFundUniverse(
                    username, password, limit, fundTypes);

            return ResponseEntity.ok(serviceResult);  // 返回 preview，而不是 funds

        } catch (Exception e) {
            return ResponseEntity.status(500).body(Map.of(
                    "status", "error",
                    "message", e.getMessage()
            ));
        }
    }


}

